package com.dsteiner.widgets.client.util;

import java.util.ArrayList;

public class ArrayUtil {

	public static String[] permute(String[] s){ //Returns a  list of permutations.
		if(s.length==1)
			return s;
		String[] array = new String[factorial(s.length)];
		ArrayList<String> x = new ArrayList<String>();
		for(int i=0; i<s.length; i++){
			String[] y=permute(remove(s,i));
			for(String z:y){
				x.add(s[i]+" "+z);
			}
		}
		return (String[])(x.toArray(array));
	}
	public static String[] remove(String[] s, int i){ //removes a String at the specified index from array s, and returns the result.
		String[] x = new String[s.length-1];
		ArrayList<String> arrayList =new ArrayList<String>();
		for(int j=0; j<s.length; j++){
			if(j!=i)
				arrayList.add(s[j]);
		}
		return (String[])(arrayList.toArray(x));
	}
	public static int factorial(int i){//Finds the factorial of an integer so we can know the size of the permutation array.
		int result=1;
		for(int y=1; y<=i; y++){
			result*=y;
		}
		return result;
	}
}
